﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	TestoDialog
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class TestoDialog : Form
    {
        public TestoDialog()
        {
            InitializeComponent();

            if (ViewQ.readViewQ == 0) return;

            dataGridView1.ColumnCount = 7;
            this.dataGridView1.Columns[0].HeaderText = "codice";
            this.dataGridView1.Columns[1].HeaderText = "denominazione";
            this.dataGridView1.Columns[2].HeaderText = "colore";
            this.dataGridView1.Columns[3].HeaderText = "font";
            this.dataGridView1.Columns[4].HeaderText = "altezza da tabelle";
            this.dataGridView1.Columns[5].HeaderText = "altezza car.";
            this.dataGridView1.Columns[6].HeaderText = "size";

            this.dataGridView1.Columns[0].Width = 70;
            this.dataGridView1.Columns[1].Width = 300;
            this.dataGridView1.Columns[2].Width = 60;
            this.dataGridView1.Columns[3].Width = 60;
            this.dataGridView1.Columns[4].Width = 60;
            this.dataGridView1.Columns[5].Width = 60;
            this.dataGridView1.Columns[6].Width = 60;

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;

            int iSel = 0;
            for (int i = 0; i < ViewQ.nSimbT; ++i)
            {
                if (ViewQ.vectT[i].selected == 0) continue;
                string[] row = new string[] {
                     ViewQ.vectT[i].codice.ToString(),ViewQ.vectT[i].denom.ToString()};
            
                 //,ViewQ.vectS[i].visib.ToString()};
                dataGridView1.Rows.Add(row);
                dataGridView1[2, iSel].Style.BackColor = ViewQ.vectT[i].icolor;
                ++iSel;
            }

            textBox1.Text = iSel.ToString();

            button1.DialogResult = DialogResult.Cancel;
            button2.DialogResult = DialogResult.OK;

            DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
            buttonColumn.HeaderText = "";
            buttonColumn.Name = "Color";
            buttonColumn.Text = "V";
            buttonColumn.UseColumnTextForButtonValue = true;
            buttonColumn.Width = 20;

            dataGridView1.Columns.Insert(3, buttonColumn);
            // Add a CellClick handler to handle clicks in the button column.
            dataGridView1.CellClick += new DataGridViewCellEventHandler(this.dataGridView1_CellClick);

            DataGridViewButtonColumn buttonColumn1 = new DataGridViewButtonColumn();
            buttonColumn1.HeaderText = "";
            buttonColumn1.Name = "Font";
            buttonColumn1.Text = "V";
            buttonColumn1.UseColumnTextForButtonValue = true;
            buttonColumn1.Width = 20;
            dataGridView1.Columns.Insert(5, buttonColumn1);

            DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                column.FlatStyle = FlatStyle.Standard;
                column.ThreeState = false;
                column.CellTemplate = new DataGridViewCheckBoxCell();
                column.CellTemplate.Style.BackColor = Color.White;
                column.HeaderText = "visibile";
            }
            int iCol = 6;
            int iCol1 = 4;
            int iCol2 = 7;
            int iCol3 = 8;
            int iCol4 = 9;
            dataGridView1.Columns.Insert(iCol, column);

            iSel = 0;
            for (int i = 0; i < ViewQ.nSimb; ++i)
            {
                if (ViewQ.vectT[i].selected == 0) continue;

                dataGridView1[iCol1, iSel].Value = ViewQ.vectT[i].iFont;
                dataGridView1[iCol3, iSel].Value = ViewQ.vectT[i].iFont.Height.ToString();
                dataGridView1[iCol4, iSel].Value = ViewQ.vectT[i].iFont.Size.ToString();
                if (ViewQ.vectT[i].visib == 0) dataGridView1[iCol, iSel].Value = false;
                else
                {
                    dataGridView1[iCol, iSel].Value = true;
                }
                dataGridView1[iCol2, iSel].Value = ViewQ.vectT[i].altezza.ToString("F0");
                ++iSel;
            }

            DataGridViewCheckBoxColumn column1 = new DataGridViewCheckBoxColumn();
            {
                //        column.HeaderText = ColumnName.OutOfOffice.ToString();
                //      column.Name = ColumnName.OutOfOffice.ToString();
                column1.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                //      DataGridViewAutoSizeColumnMode.DisplayedCells;
                column1.FlatStyle = FlatStyle.Standard;
                column1.ThreeState = false;
                column1.CellTemplate = new DataGridViewCheckBoxCell();
                column1.CellTemplate.Style.BackColor = Color.White;
                column1.HeaderText = "presente in archvio";
            }

            iCol = 10;
            dataGridView1.Columns.Insert(iCol, column1);

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                for (int i = 0; i < ViewQ.nSimb; ++i)
                {
                    if (ViewQ.vectT[i].selected == 0) continue;

                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from testo where codice_testo = '" + ViewQ.vectT[i].codice.ToString() + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                      if (reader.HasRows) dataGridView1[iCol, i].Value = true;
                    }
                }
                conn.Close();
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                ColorDialog colorDialog1 = new ColorDialog();
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    dataGridView1[2, e.RowIndex].Style.BackColor = colorDialog1.Color;
                }
            }

            if (e.ColumnIndex == 5)
            {
                FontDialog fontDialog1 = new FontDialog();
                fontDialog1.Font = (Font) dataGridView1[4, e.RowIndex].Value;
                if (fontDialog1.ShowDialog() == DialogResult.OK)
                {
                    dataGridView1[4, e.RowIndex].Value = fontDialog1.Font;
                    dataGridView1[8, e.RowIndex].Value = fontDialog1.Font.Height.ToString();
                    dataGridView1[9, e.RowIndex].Value = fontDialog1.Font.Size.ToString();
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int count = dataGridView1.Rows.Count;

            for (int i = 0; i < count; ++i)
            {
                string row = dataGridView1[0, i].Value.ToString();
                for (int j = 0; j < ViewQ.nSimbT; ++j)
                {
                    int comp = string.Compare(ViewQ.vectT[i].elemento, 0, ViewQ.selectedElement, 0, 6);
                    comp = 0;
                    if (comp == 0)
                    {
                        if (ViewQ.vectT[j].codice.Contains(row))
                        {
                            ViewQ.vectT[j].icolor = dataGridView1[2, i].Style.BackColor;
                            string ss = dataGridView1[3, i].Value.ToString();
                            ViewQ.vectT[j].iFont = stringToFont(ss);
                            if ((bool)dataGridView1[10, i].Value) ViewQ.vectT[j].visib = 1;
                            else ViewQ.vectT[j].visib = 0;
                            break;
                        }
                    }
                }
            }

            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            int count = this.dataGridView1.RowCount;
            bool bChecked = (bool)checkBox1.Checked;
            if (bChecked) 
            {
                for(int j=0;j<count;++j){
                     dataGridView1[6, j].Value = false;
                    }
            }
            else  {
                for(int j=0;j<count;++j)
                {
                    dataGridView1[6, j].Value = true;
                    }
            }

            dataGridView1.Refresh();
        
        }

 

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }


        private Font stringToFont(string ss)
        {
            float sizef = 1.0F;
            int ipos = ss.IndexOf("Size=", 0);
            if (ipos > 0)
            {
                int ipos1 = ss.IndexOf(",", ipos);
                int len = ipos1 - ipos - 5;
                string s1 = ss.Substring(ipos + 5, len);
                sizef = float.Parse(s1);
                //              MessageBox.Show(s1 + "  " + sizef.ToString()); 
            }

            Font ifont = new Font(ss, sizef);
            return (ifont);
        }

        private string aggiungiApostrofo(string orig)
        {
            string s1 = "";
            string s2 = ""; string s3 = "";
            int ipos = 0, len = 0;
            int ipos1 = 0;
            s2 = orig;

        L10: ipos = s2.IndexOf('\x27', ipos1);

            if (ipos > 0)
            {
                len = s2.Length;
                s1 = s2.Substring(0, ipos);
                s1 = string.Concat(s1, "\x27\x27");
                s3 = s2.Substring(ipos + 1, len - ipos - 1);
                s1 = string.Concat(s1, s3);
                s2 = s1;
                ipos1 = ipos + 2;
                goto L10;
            }

            return (s2);
        }

     }
}
